**************************************************************
** Replication Code for RIO Article "Resisting Democracy Assistance: Who Seeks and Receives Technical Election Assistance?"
** Last Updated: March 2016
** Inken von Borzyskowski
** 
** Input: Borzyskowski.dta
**************************************************************

* Software: Stata 12.1 SE
* Set working directory to the location of Borzyskowski.dta

set more off

use "Borzyskowski.dta", clear




* Figure 1
sort year
collapse (last) numberY_elections  numberY_provisionTechElAsstUN  numberY_requestTechElAsstUN numberY_requestObs numberY_provisionObs, by(year)

* If figure 1 will not reproduce with the full code below please use the following shorter version:
* twoway line numberY_elections numberY_requestObs numberY_provisionObs numberY_requestTechElAsstUN numberY_provisionTechElAsstUN year

twoway line numberY_elections numberY_requestObs numberY_provisionObs numberY_requestTechElAsstUN numberY_provisionTechElAsstUN year, ///
	legend(order(1 "Total Number of Elections" 2 "Request for Election Observers" ///
	 3 "Provision of Election Observers" 4 "Request for Technical El. Assistance" ///
	 5 "Provision of Technical El. Assistance") size(2.5) pos(2))  ///
	lpattern("l" "-" "shortdash" "-." "shortdash_dot") lcolor("black" "blue" "blue" "red" "red") ///
	xscale(range(1989 2005) noextend)  ///
	xlabel(1990(5)2005, labsize(3))  ///
	ylabel(,labsize(3))  ///
	xtitle("Year", size(3)) ///
	ytitle("Number of Elections", size(3)) ///
	aspectratio(0.5)
sum numberY_elections numberY_requestObs numberY_provisionObs numberY_requestTechElAsstUN numberY_provisionTechElAsstUN year
use "Borzyskowski.dta", clear




* Figure 2
use "Borzyskowski.dta", clear
graph bar numberY_requestTechElAsstUN numberY_provisionTechElAsstUN, over(year, label(angle(90))) ///
	graphregion(color(white)) plotr(m(zero)) ylabel(, angle(0))  ///
	scheme(lean1) aspectratio(0.5) ///
	legend(order(1 "Requests" 2 "Provision")  pos(12)) ///
	ytitle("Number of Elections")
	
	


* Figure 3
ssc install spineplot
by RequestTechElAsst_UN AdminCapacityDayFILLED_Lag13, sort: gen NAdmin = _N
spineplot RequestTechElAsst_UN  AdminCapacityDayFILLED_Lag13, bar1(bcolor(gs14)) bar2(bcolor(gs8)) ///
	text(NAdmin, mlabcolor(black)) ///
	graphregion(color(white)) plotr(m(zero)) scheme(lean1)
tab RequestTechElAsst_UN AdminCapacityDayFILLED_Lag13, mi
drop NAdmin




* Figure 4
by RequestTechElAsst_UN Regime, sort: gen NAut = _N
spineplot RequestTechElAsst_UN Regime, bar1(bcolor(gs14)) bar2(bcolor(gs8)) ///
	text(NAut, mlabcolor(black)) ///
	graphregion(color(white)) plotr(m(zero)) scheme(lean1)
tab RequestTechElAsst_UN Regime, mi
drop NAut




* Table 1 
use "Borzyskowski.dta", clear
ssc install sutex
sutex  RequestTechElAsst_UN ProvisionTechElAsst_UN RequestObservers ProvisionObservers ///
	AdminCapacityDayFILLED_Lag13 Regime ///
	demand2el demand2elLOG  UNbudgetconstraintRqLog ///
	WB_GDPpc_Lag1Log  DPI_oppvote_Lag1 Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log type3 Nelda_UncertainElection ///
	Nelda_OppoCompLag ProvisionTechElAsst_Other ///
	Nelda_IncConfident Snap Nelda_PreviousCondemnation WB_ODA_Lag1mLog Ross_oilgas_valuePOP2000_Lag1LOG QED_SR21CHEATd,	minmax label





* Table 2 
use "Borzyskowski.dta", clear
local controls  WB_GDPpc_Lag1Log  DPI_oppvote_Lag1 Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log i.type3 Nelda_UncertainElection
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13  ib1.Regime				 		, cluster(cowcode)
est store Tab2_mod1
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13  					`controls' , cluster(cowcode)
est store Tab2_mod2
logit RequestTechElAsst_UN ib1.Regime	 										`controls' , cluster(cowcode)
est store Tab2_mod3
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 		`controls' , cluster(cowcode)
est store Tab2_mod4
//Latex Table 2 Output
estout Tab2_mod* ///
using Tab2.tex, replace style(tex) eqlabels(none)  cells(b(fmt(%9.3f) star) ///
se(par fmt(%9.3f))) stats(N N_clust aic bic ll, fmt(0 0 2 2 2) label("Observations" "Clusters" "AIC" "BIC" "LL")) ///
starlevels(* .1 ** .05 *** .01) label




* Figure 5 (from Table 2, model 2)
local controls  WB_GDPpc_Lag1Log  DPI_oppvote_Lag1 Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log i.type3 Nelda_UncertainElection
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13  				`controls' , cluster(cowcode)
predict demand
margins, at(AdminCapacityDayFILLED_Lag13 =(1(2)3) WB_GDPpc_Lag1Log=7.026 ///
	Prio_CWPast10=0 UNPKO_dummy =0 WB_pop_Lag1Log= 16.03737 type3=0 ///
	Nelda_UncertainElection = 0 DPI_oppvote_Lag1 =15.35134 ) vsquish
* Output:
* 1  |   .2969382    .075203     3.95   0.000      .149543    .4443334
* 2  |   .1466285   .0308309     4.76   0.000     .0862011     .207056
marginsplot,   plotopts(lcolor("none")) ///
	xtitle("Election Administrative Capacity")  ///
	ytitle("Predicted Probability of Request") ///
	title("") ///
	graphregion(color(white)) plotr(m(zero)) scheme(lean1) ///
	xlabel(1 (2) 3, valuelabel)  yscale(range(0 0.5)) ylabel(#6) xscale(range(0 4))
drop demand
* Figure 5 created in R using output above
	



* Figure 6 (from Table 2, model 3)
local controls  WB_GDPpc_Lag1Log  DPI_oppvote_Lag1 Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log i.type3 Nelda_UncertainElection
logit RequestTechElAsst_UN ib1.Regime 				`controls' , cluster(cowcode)
predict demand
margins, at(Regime  =(0(1)1) WB_GDPpc_Lag1Log=7.026 ///
	Prio_CWPast10=0 UNPKO_dummy =0 WB_pop_Lag1Log= 16.03737 type3=0 ///
	Nelda_UncertainElection = 0 DPI_oppvote_Lag1 =15.35134 ) vsquish
* Output:
*  1  |    .098441   .0328891     2.99   0.003     .0339796    .1629024
*  2  |   .2212398   .0510316     4.34   0.000     .1212198    .3212599
marginsplot,   plotopts(lcolor("none")) ///
	xtitle("Regime Type")  ///
	ytitle("Predicted Probability of Request") ///
	title("") ///
	graphregion(color(white)) plotr(m(zero)) scheme(lean1) ///
	xlabel(0 (1) 1, valuelabel) yscale(range(0 0.5)) ylabel(#6) xscale(range(-0.5 1.5))
drop demand
* Figure 6 created in R using output above

	
	
	
* Table 3
local controls  i.type3 WB_GDPpc_Lag1Log UNPKO_dummy Nelda_OppoCompLag ProvisionTechElAsst_Other
logit ProvisionTechElAsst_UN demand2elLOG 	UNbudgetconstraintRqLog						  if RequestTechElAsst_UN ==1 , cluster(cowcode)
est store Tab3_mod1
logit ProvisionTechElAsst_UN demand2elLOG 										`controls'  if RequestTechElAsst_UN ==1 , cluster(cowcode)
est store Tab3_mod2
logit ProvisionTechElAsst_UN UNbudgetconstraintRqLog 							`controls'  if RequestTechElAsst_UN ==1 , cluster(cowcode)
est store Tab3_mod3
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog 				`controls'  if RequestTechElAsst_UN ==1 , cluster(cowcode)
est store Tab3_mod4
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog ib3.AdminCapacityDayFILLED_Lag13	`controls'  if RequestTechElAsst_UN ==1 , cluster(cowcode)
est store Tab3_mod5
//Latex Table 3 Output
estout Tab3_mod* ///
using Tab3.tex, replace style(tex) eqlabels(none)  cells(b(fmt(%9.3f) star) ///
se(par fmt(%9.3f))) stats(N N_clust aic bic ll, fmt(0 0 2 2 2) label("Observations" "Clusters" "AIC" "BIC" "LL")) ///
starlevels(* .1 ** .05 *** .01) label




* Figure 7 (from Table 3, model 2)
local controls  i.type3 WB_GDPpc_Lag1Log UNPKO_dummy Nelda_OppoCompLag ProvisionTechElAsst_Other
logit ProvisionTechElAsst_UN demand2elLOG 	`controls'  if RequestTechElAsst_UN ==1 , cluster(cowcode)
sum type3 WB_GDPpc_Lag1Log UNPKO_dummy Nelda_OppoCompLag ProvisionTechElAsst_Other if RequestTechElAsst_UN ==1
margins, at(demand2elLOG =(0(0.2)4) type3=0  WB_GDPpc_Lag1Log=6.384907  UNPKO_dummy =0 Nelda_OppoCompLag=1 ProvisionTechElAsst_Other =0) vsquish 
marginsplot, ///
	plot1opts(lpattern(solid)) recast(line)   ///
	xtitle("Lead Time  (logged months)")  ///
	ytitle("Predicted Probability of UN Assistance") ///
	title("") legend(off) ylab(0(0.2)1, nogrid) xlabel(0 (1) 4) ///
	graphregion(color(white) )  scheme(lean1) ///
	xline(2.26, lwidth(thick) lcolor(red) lpattern(dot)) ///
	plotregion(margin(zero))  
* correct the confidence intervals for logit scale, to be constrained by [0,1]
margins, at(demand2elLOG =(0(0.2)4) type3=0  WB_GDPpc_Lag1Log=6.384907  UNPKO_dummy =0 Nelda_OppoCompLag=1 ProvisionTechElAsst_Other =0) vsquish predict(xb)
matrix at=r(at)
matrix list at
matrix at=at[1...,"demand2elLOG"]  
matrix list at
matrix list r(table)
matrix  A = r(table)
matrix  R = (A["b",1...]  \ A["ll",1...] \ A["ul",1...])'
matrix list R
mata:
C = invlogit(st_matrix("R"))
st_replacematrix("R", C)
end
mat list R , format(%6.4f)
matrix p=at,R
matrix list p
svmat p, names(p)
clist p1-p4 in 1/21
* average lead time given request = 8.6 months; logged lead time = ln(8.6+1) =  2.26
sum  demand2el  if RequestTechElAsst_UN ==1
twoway (line p2 p1, lcolor(black)) (line p3 p1, lcolor(black) lpattern(dash)) ///
	(line p4 p1, lcolor(black) lpattern(dash)) , 	///
	xtitle("Lead Time  (logged months)")  ///
	ytitle("Predicted Probability of UN Assistance") ///
	title("") legend(off) ylab(0(0.2)1, nogrid) ///
	graphregion(color(white) )  scheme(lean1) ///
	xline(2.26, lwidth(thick) lcolor(red) lpattern(dot)) ///
	plotregion(margin(zero)) 

	
	

* Table 4
local controls1  WB_GDPpc_Lag1Log  DPI_oppvote_Lag1 Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log i.type3 Nelda_UncertainElection
local controls2  i.type3 WB_GDPpc_Lag1Log UNPKO_dummy ProvisionTechElAsst_Other
heckprob ProvisionTechElAsst_UN demand2elLOG 	UNbudgetconstraintRqLog	, ///
	select(RequestTechElAsst_UN = ib3.AdminCapacityDayFILLED_Lag13  ib1.Regime ) vce(cluster cowcode) 
est store Tab4_mod1
heckprob ProvisionTechElAsst_UN demand2elLOG `controls2' Nelda_OppoCompLag, ///
	select(RequestTechElAsst_UN = ib3.AdminCapacityDayFILLED_Lag13   `controls1') vce(cluster cowcode) 
est store Tab4_mod2
heckprob ProvisionTechElAsst_UN UNbudgetconstraintRqLog `controls2' Nelda_OppoCompLag, ///
	select(RequestTechElAsst_UN =  ib1.Regime `controls1') vce(cluster cowcode) 
est store Tab4_mod3
heckprob ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog `controls2', ///
	select(RequestTechElAsst_UN = ib3.AdminCapacityDayFILLED_Lag13  ib1.Regime `controls1') vce(cluster cowcode) 
est store Tab4_mod4
//Latex Table 4 Output
estout Tab4_mod* ///
using Tab4.tex, replace style(tex) eqlabels(none)  cells(b(fmt(%9.3f) star) ///
se(par fmt(%9.3f))) stats(N N_clust aic bic ll, fmt(0 0 2 2 2) label("Observations" "Clusters" "AIC" "BIC" "LL")) ///
starlevels(* .1 ** .05 *** .01) label






* Further robustness checks - reported in paper without table 

* Robustness checks for government request 
local controls  WB_GDPpc_Lag1Log  Prio_CWPast10 UNPKO_dummy ///
	WB_pop_Lag1Log i.type3 Nelda_UncertainElection
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 , cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 Snap, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 Ross_oilgas_valuePOP2000_Lag1LOG, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 Nelda_PreviousCondemnation, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 WB_ODA_Lag1mLog, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 QED_SR21CHEATd, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' DPI_oppvote_Lag1 Snap Ross_oilgas_valuePOP2000_Lag1LOG Nelda_PreviousCondemnation WB_ODA_Lag1mLog QED_SR21CHEATd, cluster(cowcode)
logit RequestTechElAsst_UN ib3.AdminCapacityDayFILLED_Lag13 ib1.Regime	 `controls' Nelda_IncConfident Snap Ross_oilgas_valuePOP2000_Lag1LOG Nelda_PreviousCondemnation WB_ODA_Lag1mLog QED_SR21CHEATd, cluster(cowcode)

* Robustness checks for UN assistance
local controls  i.type3 WB_GDPpc_Lag1Log UNPKO_dummy Nelda_OppoCompLag ProvisionTechElAsst_Other
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog 	`controls' 									if RequestTechElAsst_UN ==1, cluster(cowcode)
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog 	`controls' Nelda_PreviousCondemnation 		if RequestTechElAsst_UN ==1, cluster(cowcode)
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog 	`controls' WB_ODA_Lag1mLog 					if RequestTechElAsst_UN ==1 , cluster(cowcode)
logit ProvisionTechElAsst_UN demand2elLOG UNbudgetconstraintRqLog 	`controls' Nelda_PreviousCondemnation WB_ODA_Lag1mLog if RequestTechElAsst_UN ==1 , cluster(cowcode)

